<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scoped">
              <el-button type="text" @click="hAdd(2,scoped.row.id)">添加</el-button>
              <el-button type="text" @click="doEdit(scoped.row.id)">编辑</el-button>
              <el-button type="text" @click="doDelete(scoped.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog :visible.sync="showDialog" :title="(isEdit?'编辑':'添加')+'权限' ">
      <!-- 表单内容 -->
      <el-form ref="formData" label-width="100px" :model="formData">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="hSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
// 获取权限列表,删除权限,添加权限,获取权限详情,更新权限
import { getPermissionList, delPermission, addPermission, getPermissionDetail, updatePermission } from '@/api/permisson'
// 数组转树
import { tranListToTreeData } from '@/utils'
export default ({
  data() {
    return {
      // 对话框状态
      isEdit: '',
      // 权限列表
      list: [],
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 获取权限列表
    async loadPermissionList() {
      const res = await getPermissionList()
      console.log(res)
      // 绑定权限列表
      this.list = tranListToTreeData(res.data, '0')
    },
    // 删除权限
    doDelete(id) {
      this.$confirm('你确定要删除吗？').then(async() => {
        const res = await delPermission(id)
        console.log(res)
        // 删除成功
        this.$message.success(res.message)
        // 重新拉取数据
        this.loadPermissionList()
      }).catch(() => {

      })
    },
    // 点击添加权限
    hAdd(type, id) {
      // 清空表单数据
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
      // 改变对话框状态
      this.isEdit = false
      // 弹出对话框
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = id
    },
    // 添加权限
    async doAdd() {
      const res = await addPermission(this.formData)
      // 提交成功
      this.$message.success(res.message)
      // 关闭对话框
      this.showDialog = false
      // 重新拉取数据
      this.loadPermissionList()
      console.log(res)
    },
    // 确定提交
    hSubmit() {
      // 判断当前对话框状态 true为编辑,false为添加
      this.isEdit ? this.updatEdit() : this.doAdd()
      // 重新拉取数据
      this.loadPermissionList()
    },
    // 点击编辑
    async doEdit(id) {
      // 改变对话框状态
      this.isEdit = true
      // 打开对话框
      this.showDialog = true
      // 获取权限详情
      const res = await getPermissionDetail(id)
      console.log(res)
      // 数据回调
      this.formData = res.data
    },
    // 更新权限
    async updatEdit() {
      await updatePermission(this.formData)
      // 更新成功
      this.$message.success('更新权限成功')
      // 关闭对话框
      this.showDialog = false
    }
  }
})
</script>
